package com.example.common.base;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;

import com.example.common.R;
import com.example.common.interfaces.LifeCycleListener;
import com.example.common.utils.ClickUtils;
import com.example.common.utils.L;
import com.example.common.utils.UIStatusBarHelper;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

public abstract class AbsActivity<B extends ViewDataBinding> extends AppCompatActivity {

    protected String TAG;
    protected Context mContext;
    protected B mBinding;
    protected List<LifeCycleListener> mLifeCycleListeners;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        UIStatusBarHelper.translucent(this);
        UIStatusBarHelper.setStatusBarDarkMode(this);
        L.e("当前Activity==" + getClass().getName());
        TAG = this.getClass().getSimpleName();
        mContext = this;
        mBinding = DataBindingUtil.setContentView(this, getLayoutId());
        main();
        initListener();
        mLifeCycleListeners = new ArrayList<>();
        if (mLifeCycleListeners != null) {
            for (LifeCycleListener lifeCycleListener : mLifeCycleListeners) {
                lifeCycleListener.onCreate();
            }
        }
    }

    protected abstract int getLayoutId();

    protected abstract void main();

    protected abstract void initListener();

    protected void setTitle(String title) {
        TextView titleView = findViewById(R.id.titleView);
        if (titleView != null) {
            titleView.setText(title);
        }
    }

    public void backClick(View view) {
        if (view.getId() == R.id.btn_break) {
            onBackPressed();
        }
    }

    protected boolean canClick() {
        return ClickUtils.canClick();
    }

    /**
     * 设置透明状态栏
     */
    private void setStatusBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            if (isStatusBarWhite()) {
                window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            } else {
                window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
            }
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(0);
        }
    }

    private boolean isStatusBarWhite() {
        return false;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mLifeCycleListeners != null) {
            for (LifeCycleListener listener : mLifeCycleListeners) {
                listener.onDestroy();
            }
            mLifeCycleListeners.clear();
            mLifeCycleListeners = null;
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (mLifeCycleListeners != null) {
            for (LifeCycleListener listener : mLifeCycleListeners) {
                listener.onStart();
            }
        }
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        if (mLifeCycleListeners != null) {
            for (LifeCycleListener listener : mLifeCycleListeners) {
                listener.onReStart();
            }
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (mLifeCycleListeners != null) {
            for (LifeCycleListener listener : mLifeCycleListeners) {
                listener.onResume();
            }
        }
        //友盟统计  MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (mLifeCycleListeners != null) {
            for (LifeCycleListener listener : mLifeCycleListeners) {
                listener.onPause();
            }
        }
        //友盟统计  MobclickAgent.onPause(this);
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (mLifeCycleListeners != null) {
            for (LifeCycleListener listener : mLifeCycleListeners) {
                listener.onStop();
            }
        }
    }

    public void addLifeCycleListener(LifeCycleListener cycleListener) {
        if (mLifeCycleListeners != null && cycleListener != null) {
            mLifeCycleListeners.add(cycleListener);
        }
    }

    public void removeLifeCycleListener(LifeCycleListener cycleListener) {
        if (mLifeCycleListeners != null) {
            mLifeCycleListeners.remove(cycleListener);
        }
    }

    public void startActivity(Class<? extends Activity> cs) {
        Intent intent = new Intent(this, cs);
        startActivity(intent);
    }

}
